package com.avocado.base.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.TableGenerator;

import com.avocado.base.enums.CertificateType;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.avocado.base.enums.Gender;
import com.avocado.base.enums.Sexuality;
import com.avocado.commons.utils.JsonDateSerializer;

/**
 * 用户实体
 * @author xuwenfeng
 *
 */
@Data
@Entity
@Table(name="AVO_USER")
@JsonAutoDetect
@JsonIgnoreProperties(value={"password","payPassword","payPassword","userRoles","organizationUserRelationshipes","handler","hibernateLazyInitializer"})
public class User implements Serializable {
	
	private static final long serialVersionUID = -5178830032814719224L;

	@Id@Column(name="ID",nullable=false)
	@GeneratedValue(strategy = GenerationType.TABLE,generator="id_gen")
    @TableGenerator(name = "id_gen",pkColumnValue="USER_ID",table="AVO_ID_GENERATOR", pkColumnName="GEN_NAME",valueColumnName="GEN_VALUE",allocationSize=1)
	private Long id;
	
	@Column(name="PARENT_ID")
	private long parentId=0;//父级ID

	@Column(name="NAME",unique=true,nullable=false)
	private String name;
	
	@Column(name="WECHAT_ID",unique=true)
	private String wechatId;
	
	@Column(name="PASSWORD")
	private String password;
	
	@Column(name="CREATE_TIME")
	private Date createTime;
	
	@Column(name="UPDATE_TIME")
	private Date updateTime;

	@Column(name="ENABLE")
	private Boolean enable;//账号启用
	
	@Column(name="DELETED")
	private Boolean deleted=false;//是否软删除
	
	@Column(name="DELETED_TIME")
	private Date deletedTime;//删除时间
	
	@Column(name="ACCOUNT_NON_EXPIRED")
	private Boolean accountNonExpired;//账号未过期
	
	@Column(name="CREDENTIALS_NON_EXPIRED")
	private Boolean credentialsNonExpired;//密码未过期
	
	@Column(name="ACCOUNT_NON_LOCKED")
	private Boolean accountNonLocked;//账号未锁定
	
	@JsonSerialize(using=JsonDateSerializer.class)
	@Column(name="ACCOUNT_EXPIRED_TIME")
	private Date accountExpiredTime;
	
	@Column(name="CREDENTIALS_EXPIRED_TIME")
	private Date credentialsExpiredTime;
	
	@Column(name="ACCOUNT_LOCKED_TIME")
	private Date accountLockedTime;
	
	@Enumerated@Column(name="GENDER")
	private Gender gender;
	
	@Column(name="EMAIL")
	private String email="";
	
	@Column(name="PHONE")
	private String phone="";
	
	@Column(name="MOBILE_PHONE")
	private String mobilePhone="";
	
	@Column(name="ADDRESS")
	private String address="";
	
	@Column(name="REAL_NAME")
	private String realName="";
	
	@Column(name="COMPANY")
	private String company="";
	
	@Column(name="BALANCE")
    private BigDecimal balance=new BigDecimal("0.00");//当前余额
	
	@Column(name="PAY_PASSWORD")
    private String payPassword;//支付密码
	
	@Column(name="DEALED_SUCCESS_RATE")
    private Double dealedSuccessRate=0.00D;
	
	@Column(name="FAVORABLE_RATE")
    private Double favorableRate=0.00D;
	
	@Column(name="IS_INFO_CONFIRM")
    private Boolean isInfoConfirm;
	
	@Enumerated@Column(name="SEXUALITY")
    private Sexuality sexuality=Sexuality.heterosex;
	
	@Column(name="BIRTHDAY")
	private Date birthday;
	
	@Column(name="PHOTO",length = 1000)
	private String photo;
	
	@ManyToOne
	@JoinColumn(name="ORGANIZATION_ID",nullable=true)
	private Organization organization;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER,mappedBy="user")
	private Set<UserRole> userRoles=new HashSet<UserRole>();
	
	@Column(name="NICKNAME")
	private String nickname;
	
	@Column(name="PROVINCE")
	private String province;
	
	@Column(name="CITY")
	private String city;
	
	@Column(name="COUNTRY")
	private String country;

	@Enumerated@Column(name="CERTIFICATE_TYPE")
	private CertificateType certificateType;

	public Long getId() {
		return id;
	}

	@JsonSerialize(using= JsonDateSerializer.class)
	public Date getCreateTime() {
		return createTime;
	}
	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		User user = (User) o;
		return Objects.equals(id, user.id);
	}

	@Override
	public int hashCode() {
		return Objects.hash(id);
	}
}
